package com.raymond.smgp.msg.request;

import com.raymond.client.constants.GlobalConstants;
import com.raymond.client.msg.AbstractMessage;
import com.raymond.smgp.enums.CommandIdType;
import com.raymond.smgp.msg.tlv.TLV;
import io.netty.buffer.ByteBuf;

import java.util.ArrayList;
import java.util.List;

/**
 * 状态报告和上行请求
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-06 10:45
 */
public class DeliverRequestMessage extends AbstractMessage {
    /** 流水号 **/
    private String msgId;

    /** 是否是状态报告 **/
    private boolean isReport;

    private byte msgFmt;

    private String recvTime = GlobalConstants.EMPTY_STRING;

    private String srcTermId = GlobalConstants.EMPTY_STRING;

    private String destTermId = GlobalConstants.EMPTY_STRING;

    private short msgLength = 140;

    private byte[] msgContent = GlobalConstants.EMPTY_BYTES;

    private String reserve = GlobalConstants.EMPTY_STRING;

    /** 状态报告响应实体。 **/
    private ReportRequestMessage reportRequestMessage;

    private List<TLV> tlvList = new ArrayList<>();

    public DeliverRequestMessage(int sequenceId) {
        super(sequenceId);
    }

    @Override
    public int getCommandId() {
        return CommandIdType.DELIVER_REQUEST.getCommandId();
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public boolean isReport() {
        return isReport;
    }

    public void setReport(boolean report) {
        isReport = report;
    }

    public byte getMsgFmt() {
        return msgFmt;
    }

    public void setMsgFmt(byte msgFmt) {
        this.msgFmt = msgFmt;
    }

    public String getRecvTime() {
        return recvTime;
    }

    public void setRecvTime(String recvTime) {
        this.recvTime = recvTime;
    }

    public String getSrcTermId() {
        return srcTermId;
    }

    public void setSrcTermId(String srcTermId) {
        this.srcTermId = srcTermId;
    }

    public String getDestTermId() {
        return destTermId;
    }

    public void setDestTermId(String destTermId) {
        this.destTermId = destTermId;
    }

    public short getMsgLength() {
        return msgLength;
    }

    public void setMsgLength(short msgLength) {
        this.msgLength = msgLength;
    }

    public byte[] getMsgContent() {
        return msgContent;
    }

    public void setMsgContent(byte[] msgContent) {
        this.msgContent = msgContent;
    }

    public String getReserve() {
        return reserve;
    }

    public void setReserve(String reserve) {
        this.reserve = reserve;
    }

    public ReportRequestMessage getReportRequestMessage() {
        return reportRequestMessage;
    }

    public void setReportRequestMessage(ReportRequestMessage reportRequestMessage) {
        this.reportRequestMessage = reportRequestMessage;
    }

    public List<TLV> getTlvList() {
        return tlvList;
    }

    public void addTlv(TLV tlv) {
        this.tlvList.add(tlv);
    }

    /**
     * 可选参数tlv部分 解码
     *
     * @param bodyBuffer
     * @throws Exception
     */
    public void decodeTLV(ByteBuf bodyBuffer) {
        while (bodyBuffer.readableBytes() > 0) {
            short tag = bodyBuffer.readShort();
            short length = bodyBuffer.readShort();
            byte[] valveBytes = new byte[length];
            bodyBuffer.readBytes(valveBytes);
            TLV tlv = TLV.getTLV(tag);
            tlv.setValueData(valveBytes);
        }
    }
}
